package com.feicheng.lanniao.service.impl;

import com.feicheng.lanniao.entity.House;
import com.feicheng.lanniao.entity.HouseOwner;
import com.feicheng.lanniao.entity.HouseSell;
import com.feicheng.lanniao.entity.HouseSellOwner;
import com.feicheng.lanniao.exception.SystemException;
import com.feicheng.lanniao.mapper.HouseSellMapper;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.IHouseOwnerService;
import com.feicheng.lanniao.service.IHousePropertTypeService;
import com.feicheng.lanniao.service.IHouseSellOwnerService;
import com.feicheng.lanniao.service.IHouseSellService;
import com.feicheng.lanniao.service.IHouseSellVoService;
import com.feicheng.lanniao.service.IHouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-02
 */
@Service
public class HouseSellServiceImpl extends ServiceImpl<HouseSellMapper, HouseSell> implements IHouseSellService {

	@Autowired
	private IHouseService iHouseService;
	@Autowired
	private IHouseSellOwnerService iHouseSellOwnerService;
	@Autowired
	private IHousePropertTypeService iHousePropertTypeService;

	// 2-- 新增1个卖房信息(1个卖房对象)HouseSell、House、HouseSellOwner
	/**
	 * @see iHouseSellOwnerService.addHouseSell
	 */
	@Override
	@Transactional(rollbackForClassName = { "Exception" })
	public ResultInfo addHouseSell(
			Integer totalPrice, Integer propertType, String propertYear,
			Integer clientId, 
			Integer roomCount, Integer area, Integer provienceId, Integer cityId, Integer reignId, String imgUrl,
			String tel,
			String wxNumber,
			String louceng,
			String xiaoqu,
			String pianqu,
			String chaoxiang,
			String zhuangxiu,
			Integer dianti,
			String niandai
	){
		House house = new House();		
		house.setRoomCount(roomCount);
		house.setArea(area);
		if (null == house.getArea()) {
			throw new SystemException("面积不能为空，请输入一下");
		}
		house.setProvienceId(provienceId);
		if (null == house.getProvienceId()) {
			throw new SystemException("省不能为空，请输入一下");
		}
		house.setCityId(cityId);
		if (null == house.getCityId()) {
			throw new SystemException("市不能为空，请输入一下");
		}
		house.setReignId(reignId);
		if (null == house.getReignId()) {
			throw new SystemException("区不能为空，请输入一下");
		}
		house.setPianqu(pianqu);
		house.setXiaoqu(xiaoqu);
		house.setZhuangxiu(zhuangxiu);
		house.setChaoxiang(chaoxiang);
		house.setDianti(dianti);
		house.setImgUrl(imgUrl);
		house.setNiandai(niandai);
		iHouseService.save(house);
		HouseSell houseSell = new HouseSell();
		houseSell.setHouseId(house.getHouseId());
		houseSell.setTotalPrice(totalPrice);
		houseSell.setTel(tel);
		houseSell.setWxNumber(wxNumber);
		houseSell.setLouceng(louceng);
		if (null == totalPrice) {
			throw new SystemException("总价不能为空，请输入一下");
		}
		houseSell.setPropertType(propertType);
		houseSell.setPropertYear(propertYear);
		houseSell.setClientId(clientId);
		this.save(houseSell);
		HouseSellOwner houseSellOwner = new HouseSellOwner();
		houseSellOwner.setClientId(clientId);
		houseSellOwner.setSellId(houseSell.getSellId());
		iHouseSellOwnerService.save(houseSellOwner);
		return ResultInfo.success();
	}

	// 3-- 修改1个卖房信息(1个卖房对象 , 先查询 再修改)
	@Override
	public ResultInfo editHouseSell(Integer sellId,  Integer totalPrice, Integer propertType,
			 String propertYear,  Integer roomCount, Integer area, Integer provienceId, Integer cityId,
			 Integer reignId, String imgUrl, 
			 String tel,
			 String wxNumber,
			 String louceng,
			 String xiaoqu,
			 String pianqu,
			 String chaoxiang,
			 String zhuangxiu,
			 Integer dianti,
			 String niandai) {
		HouseSell houseSell = this.getById(sellId);
		houseSell.setTotalPrice(totalPrice);
		houseSell.setPropertType(propertType);
		houseSell.setPropertYear(propertYear);
		houseSell.setTel(tel);
		houseSell.setWxNumber(wxNumber);
		houseSell.setLouceng(louceng);	
		House house = iHouseService.getById(houseSell.getHouseId());	
		house.setRoomCount(roomCount);
		house.setArea(area);
		house.setProvienceId(provienceId);
		house.setCityId(cityId);
		house.setReignId(reignId);
		house.setImgUrl(imgUrl);
		house.setPianqu(pianqu);
		house.setXiaoqu(xiaoqu);
		house.setZhuangxiu(zhuangxiu);
		house.setChaoxiang(chaoxiang);
		house.setDianti(dianti);
		house.setNiandai(niandai);
		iHouseService.updateById(house);
		this.updateById(houseSell);
		return ResultInfo.success();
	}

	// 5-- 删除1个卖房信息(假删除 修改状态，需要1个HouseSell对象)
	@Override
	public Boolean remove(HouseSell houseSell) {
		houseSell.setDelFlg(false);
		return this.updateById(houseSell);
	}

}
